<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePermissionTables extends Migration
{
    /**
     * 权限相关表
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $tableNames = config('permission.table_names');
        $columnNames = config('permission.column_names');

        Schema::create($tableNames['permissions'], function (Blueprint $table) {
            $table->bigIncrements('id')->comment('主键AutoID');
            $table->string('name')->default('')->comment('英文标识');
            $table->string('title')->default('')->comment('标题');
            $table->unsignedBigInteger('parent_id')->default(0)->comment('上级权限ID');
            $table->string('guard_name')->default('')->comment('所属门卫');
            $table->unsignedTinyInteger('is_enable')->default(0)->comment('是否启用');
            $table->unsignedInteger('created_time')->default(0)->comment('创建时间');
            $table->unsignedInteger('updated_time')->default(0)->comment('更新时间');
            $table->engine = 'InnoDB';
        });

        Schema::create($tableNames['roles'], function (Blueprint $table) {
            $table->bigIncrements('id')->comment('主键AutoID');
            $table->string('name')->default('')->comment('组名');
            $table->string('guard_name')->default('')->comment('所属门卫');
            $table->unsignedTinyInteger('is_super')->default(0)->comment('是否超级管理组');
            $table->unsignedTinyInteger('is_enable')->default(0)->comment('是否启用');
            $table->unsignedInteger('created_time')->default(0)->comment('创建时间');
            $table->unsignedInteger('updated_time')->default(0)->comment('更新时间');
            $table->engine = 'InnoDB';
        });

        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
            $table->unsignedBigInteger('permission_id')->default(0)->comment('权限ID To permissions');

            $table->string('model_type', 191)->default('')->comment('模型类型');
            $table->unsignedBigInteger($columnNames['model_morph_key'])->default(0)->comment('模型关联的ID');
            $table->index([$columnNames['model_morph_key'], 'model_type']);

            $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
                'model_has_permissions_permission_model_type_primary');
            $table->engine = 'InnoDB';
        });

        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
            $table->unsignedBigInteger('role_id')->default(0)->comment('组ID To roles');

            $table->string('model_type', 191)->default('')->comment('模型类型');
            $table->unsignedBigInteger($columnNames['model_morph_key'])->default(0)->comment('模型关联的ID');
            $table->index([$columnNames['model_morph_key'], 'model_type',]);

            $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
                'model_has_roles_role_model_type_primary');
            $table->engine = 'InnoDB';
        });

        Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
            $table->unsignedBigInteger('permission_id')->default(0)->comment('权限ID To permissions');
            $table->unsignedBigInteger('role_id')->default(0)->comment('组ID To roles');

            $table->primary(['permission_id', 'role_id']);
            $table->engine = 'InnoDB';
        });

        Schema::create($tableNames['role_has_menus'], function (Blueprint $table) {
            $table->unsignedBigInteger('role_id')->default(0)->comment('组ID To roles');
            $table->unsignedBigInteger('menu_id')->default(0)->comment('菜单ID To menus');

            $table->primary(['role_id', 'menu_id']);
            $table->engine = 'InnoDB';
        });

        app('cache')
            ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
            ->forget(config('permission.cache.key'));
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $tableNames = config('permission.table_names');

        Schema::drop($tableNames['role_has_permissions']);
        Schema::drop($tableNames['model_has_roles']);
        Schema::drop($tableNames['model_has_permissions']);
        Schema::drop($tableNames['roles']);
        Schema::drop($tableNames['permissions']);
        Schema::drop($tableNames['role_has_menus']);
    }
}
